package com.up.upzl.biz.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.ImmutableMap;
import com.up.upzl.biz.domain.Area;
import com.up.upzl.biz.domain.PaiDianRecord;
import com.up.upzl.biz.domain.Shop;
import com.up.upzl.biz.domain.Shop.CreateType;
import com.up.upzl.biz.domain.Shop.ShopFlow;
import com.up.upzl.biz.service.AreaService;
import com.up.upzl.biz.service.PaiDianRecordService;
import com.up.upzl.biz.service.SequenceService;
import com.up.upzl.biz.service.ShopService;

/**
 * 拍店控制器
 * 
 * @author geeks
 *
 */
@Controller
@RequestMapping("paiDianRecord")
public class PaiDianRecordController {

	@Autowired
	private PaiDianRecordService paiDianRecordService;

	@Autowired
	private AreaService areaService;

	@Autowired
	private ShopService shopService;

	@Value("${up.site:}")
	private String siteCode;
	@Autowired
	private SequenceService sequenceService;

	@RequestMapping("/imgstream")
	public void IO(String file, HttpServletResponse response) throws IOException {
		// 读取本地图片输入流
		FileInputStream inputStream = new FileInputStream(new File(file));
		int i = inputStream.available();
		// byte数组用于存放图片字节数据
		byte[] buff = new byte[i];
		inputStream.read(buff);
		// 记得关闭输入流
		inputStream.close();
		// 设置发送到客户端的响应内容类型
		response.setContentType("image/*");
		OutputStream out = response.getOutputStream();
		out.write(buff);
		// 关闭响应输出流
		out.close();
	}

	@GetMapping("list")
	public String list(Integer area, Integer businessArea, PaiDianRecord.HuanXian huanXian, String phone,
			String wechatId, Date dateBegin, Date dateEnd, PaiDianRecord.Status status, Pageable page, Model model,
			String huanxian1) {
		if (status == null) {
			status = PaiDianRecord.Status.待审核;
		}
		Page<PaiDianRecord> list = paiDianRecordService.findByCondition(area, businessArea, huanXian, phone, wechatId,
				dateBegin, dateEnd, status, page, huanxian1);

		model.addAttribute("pageList", list);

		Long parentId = null;
		if (siteCode.equals("bj")) {
			parentId = 36L;
		} else if (siteCode.equals("tj")) {
			parentId = 37L;
		} else {
			parentId = 36L;
		}

		List<Area> areas = areaService.findByParentId(parentId);

		model.addAttribute("areas", areas);

		return "paiDianRecord/list";
	}

	@GetMapping("updatePage")
	public String updatePage(Long id, Model model) {
		PaiDianRecord paiDianRecord = paiDianRecordService.findOne(id);

		model.addAttribute("paiDianRecord", paiDianRecord);

		return "paiDianRecord/update";
	}

	@PostMapping("yes")
	public @ResponseBody String yes(Long id, String sel) {
		if (id != null) {
			PaiDianRecord paiDianRecord = paiDianRecordService.findOne(id);
			paiDianRecord.setHuanxian1(sel);
			PaiDianRecord saveAndFlush = paiDianRecordService.saveAndFlush(paiDianRecord);
			if (saveAndFlush != null) {
				return "确认成功";
			}
		} else {
			return "id参数错误";
		}
		return null;
	}

	@PostMapping("update")
	public @ResponseBody Map<String, String> update(Long id, Integer flag, String reason, Model model) {
		PaiDianRecord paiDianRecord = paiDianRecordService.findOne(id);
		if (flag == 0) {
			paiDianRecord.setReason(reason);
			paiDianRecord.setStatus(PaiDianRecord.Status.未通过);
			paiDianRecordService.save(paiDianRecord);
		} else {
			paiDianRecord.setStatus(PaiDianRecord.Status.已通过);
			paiDianRecord.setJiangjinStatus(PaiDianRecord.JiangjinStatus.待发放);
			paiDianRecordService.save(paiDianRecord);
//			Shop shop = new Shop();
//			shop.setPhone(paiDianRecord.getPhone());
//			shop.setArea(paiDianRecord.getArea());
//			shop.setBusinessArea(paiDianRecord.getBusinessArea());
//			shop.setImgs(paiDianRecord.getImg0());
//			shop.setImgs(paiDianRecord.getImg1());
//			shop.setImgs(paiDianRecord.getImg2());
//			shop.setImgs(paiDianRecord.getImg3());
//			shop.setShopFlow(ShopFlow.待认领);
//
//			StringBuffer name = new StringBuffer();
//			if (shop.getArea() != null) {
//				name.append(shop.getArea().getName());
//			}
//			if (shop.getBusinessArea() != null) {
//				name.append(shop.getBusinessArea().getName());
//			}
//			if (shop.getUseArea() != null) {
//				name.append(shop.getUseArea());
//			}
//			shop.setName(name.toString());
//			shop.setCreateType(CreateType.拍店);
//			shop.setOnlineStatus(0);
//			Long shopNumber = sequenceService.nextVal("sq_shopNumber");
//			shop.setShopNumber("0" + shopNumber);
//			shopService.save(shop);
		}

		return ImmutableMap.of("status", "ok");
	}
}
